package elitech.vietnam.weatherfashion.backgroundtask;

import java.io.IOException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import elitech.vietnam.weatherfashion.R;
import elitech.vietnam.weatherfashion.RegisterActivity;
import elitech.vietnam.weatherfashion.config.Const;
import elitech.vietnam.weatherfashion.service.PHPSoapServiceCaller;

public class RegisterTask extends AsyncTask<String, String, Integer> {

	private RegisterActivity activity;
	private String user, pass;
	private ProgressDialog progress;

	public RegisterTask(RegisterActivity activity) {
		this.activity = activity;
	}

	@Override
	protected Integer doInBackground(String... params) {
		user = params[0];
		pass = params[1];

		try {
			return new PHPSoapServiceCaller().createNewMemberCountry(user,
					pass, "", "", "", Const.COUNTRY_CODE);
		} catch (IOException e) {
			e.printStackTrace();
			return -2;
		} catch (Exception e) {
			e.printStackTrace();
			return -2;
		}
	}

	@Override
	protected void onPostExecute(Integer result) {
		String msg = "";
		switch (result) {
		case -2:
			msg = activity.getString(R.string.cannot_connect_to_server);
			break;
		case -1:
			msg = activity.getString(R.string.register_user_exist);
			break;
		case 0:
			msg = activity.getString(R.string.register_failed);
			break;
		}
		activity.setError(msg);
		if (result > 0) {
			Intent i = new Intent();
			i.putExtra("username", user);
			i.putExtra("password", pass);
			activity.setResult(Activity.RESULT_OK, i);
			activity.finish();
		}
		progress.dismiss();

		this.activity = null;
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		progress = new ProgressDialog(activity);
		progress.setTitle(activity.getString(R.string.processing));
		progress.setMessage(activity.getString(R.string.please_wait_));
		progress.show();
	}

}
